<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *工人管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$tabs=array(
	array('op'=>'list','title'=>'工人列表'),
	array('op'=>'edit','title'=>'添加工人'),	
);
if($_RQ['op']=='list'){
	$params=array();
	$params['uniacid']=$_SESSION['uniacid'];
	if(!empty($_RQ['name'])){
		$params['name like']='%'.trim($_RQ['name']).'%';
		$return['params']['name']=$_RQ['name'];
	}
	if(isset($_RQ['phone'])){
		$params['phone']=intval($_RQ['phone']);
		$return['params']['phone']=$_RQ['phone'];
	}
	if(isset($_RQ['status'])){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$list=pdo_getslice('jy_farmer_worker',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$item['member']=cfc('core')->member_show($item['mid']);
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','edit'=>true,'list'=>array(
			array('name'=>'name','title'=>'工人姓名','type'=>'string'),
			array('name'=>'member','title'=>'绑定会员','type'=>'member'),
			array('name'=>'isanimal','title'=>'是否管理家禽','type'=>'switch','set'=>'set'),
			array('name'=>'island','title'=>'是否管理种植','type'=>'switch','set'=>'set'),
			array('name'=>'isguoshu','title'=>'是否管理果树','type'=>'switch','set'=>'set'),
			array('name'=>'status','title'=>'状态','type'=>'switch','set'=>'set'),
		)),
		'search_one'=>array('name'=>'name'),
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	$return=array();
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_farmer_worker',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		
	}else{
		$item=array();
	}
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回工人列表'),
		),
		'edit_tab'=>true,
		'edits'=>array(
			array(
				'title'=>'基本信息',
				'list'=>array(
					array('name'=>'name','title'=>'工人姓名','type'=>'string'),
					array('name'=>'group_id','title'=>'选择分类','type'=>'select','options'=>'group','option_title'=>'title'),
					array('type'=>'choose','title'=>'选择会员','name'=>'mid','action'=>'get_member','choose_title'=>'选择会员'),
					array('name'=>'isguoshu','title'=>'是否管理果树','type'=>'switch'),
					array('name'=>'gclassify_id','title'=>'添加管理果树','type'=>'checkbox','options'=>'gclassify','show'=>array('isguoshu'=>1)),
					array('name'=>'island','title'=>'是否管理种植','type'=>'switch'),
					array('name'=>'lclassify_id','title'=>'添加管理地块','type'=>'checkbox','options'=>'lclassify','show'=>array('island'=>1)),
					array('name'=>'isanimal','title'=>'是否管理家禽','type'=>'switch'),
					array('name'=>'aclassify_id','title'=>'添加管理家禽分类','type'=>'checkbox','options'=>'aclassify','show'=>array('isanimal'=>1)),
					array('name'=>'status','title'=>'状态','type'=>'switch'),
				)
			),
		)
	);
	$gclassify=pdo_getall('jy_farmer_gclassify',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$lclassify=pdo_getall('jy_farmer_lclassify',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$aclassify=pdo_getall('jy_farmer_aclassify',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['group']=pdo_getall('jy_farmer_worker_group',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	foreach($gclassify as &$row){
		$row=$row['title'];
	}
	foreach($lclassify as &$row){
		$row=$row['title'];
	}
	foreach($aclassify as &$row){
		$row=$row['title'];
	}
	$return['gclassify']=$gclassify;
	$return['lclassify']=$lclassify;
	$return['aclassify']=$aclassify;
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	//exi($postdata['lev_name'],'error');exit;
	$data=array(
		'name'=>trim($postdata['name']),
		'isguoshu'=>intval($postdata['isguoshu']),
		'island'=>intval($postdata['island']),
		'isanimal'=>intval($postdata['isanimal']),
		'gclassify_id'=>trim($postdata['gclassify_id']),
		'lclassify_id'=>trim($postdata['lclassify_id']),
		'aclassify_id'=>trim($postdata['aclassify_id']),
		'status'=>intval($postdata['status']),
		'group_id'=>intval($postdata['group_id']),
		'mid'=>intval($postdata['mid']),
	);
	$id=intval($postdata['id']);
	if($id){
		pdo_update('jy_farmer_worker',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		pdo_insert('jy_farmer_worker',$data);
	}
	exi('操作成功！','','list');
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_farmer_worker',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择要编辑的工人！','error');
	}
	$post=get_postdata();
	if(isset($post['status'])){
		pdo_update('jy_farmer_worker',array('status'=>intval($post['status'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	if(isset($post['island'])){
		pdo_update('jy_farmer_worker',array('island'=>intval($post['island'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	if(isset($post['isanimal'])){
		pdo_update('jy_farmer_worker',array('isanimal'=>intval($post['isanimal'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	exi('操作成功！');
}